package GUI;

import java.awt.Rectangle;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

import Client_logic.ConnectionToServer;
import Client_logic.Customer;

/**
 * This class use to search for customer.
 * @author ido
 *
 */
public class CustomerSearch extends JFrame
{
	private JComboBox cIDComboBox = null;
	private JLabel cIDlLabel = null;
	private JButton searchButton = null;
	private JPanel jContentPane = null;
	private CustomerForm customerForm = null;
	private Customer customer = null;
	private long cID = 0;
	private String initialitoForm;	
	
	/**
	 * This is the default constructor
	 */
	public CustomerSearch(String initialitoForm) 
	{
		super();
		this.initialitoForm = initialitoForm;
		initialize();		
	}
	
	/**
	 * This method initializes ProductForm
	 */
	private void initialize() 
	{
		customer = new Customer(0, "", 0, 0,"");
		this.setContentPane(getJContentPane());
		this.setTitle("Customer SEARCH");
	}
	
	private JPanel getJContentPane() 
	{
		if (jContentPane == null) 
		{
			jContentPane = new JPanel();
			jContentPane.setLayout(null);
			
			cIDlLabel = new JLabel();
			cIDlLabel.setBounds(new Rectangle(15, 17, 113, 22));
			cIDlLabel.setText("Client ID:");
			jContentPane.setLayout(null);
			jContentPane.add(cIDlLabel, null);
			jContentPane.add(getCIDComboBox(), null);
			jContentPane.add(getSearchButton(),null);
			customerForm = new CustomerForm();
			customerForm.getCancelButton().addActionListener(new java.awt.event.ActionListener() 
			{
				public void actionPerformed(java.awt.event.ActionEvent e) 
				{
					setSize(300, 150);
					setContentPane(getJContentPane());								
				}
			});			
		}
		return jContentPane;		
	}
	
	/**
	 * This method initializes PIDComboBox	
	 */
	private JComboBox getCIDComboBox()
	{
		if(cIDComboBox == null)
		{	
			cIDComboBox = new JComboBox();
			cIDComboBox.setBounds(new Rectangle(120, 17, 107, 25));
			long[] customerid = null;
			try
			{
				ConnectionToServer connectiontoserver = new ConnectionToServer();		
				customerid = (long[]) connectiontoserver.loadAllCustomers();
				if (customerid == null)
				{
					JOptionPane.showMessageDialog(null, "Clients is EMPTY!!!");
					cIDComboBox.addItem(0);
				}	
				else
				{
					int len = customerid.length;
					if (len > 0)
						cID = customerid[0];
					for(int i=0; i < len; i++)
					{
						cIDComboBox.addItem(customerid[i]);
					}	
				}
			}
			catch(Exception e)
			{
				cIDComboBox.addItem(0);
			}
			cIDComboBox.addActionListener(new java.awt.event.ActionListener() 
			{
				public void actionPerformed(java.awt.event.ActionEvent e) 
				{
					cID = (Long)cIDComboBox.getSelectedItem();	
				}	
			});	
		}	
		return cIDComboBox;
	}

	
	/**
	 * This method initializes searchButton	
	 */
	private JButton getSearchButton() 
	{
		if (searchButton == null) 
		{
			searchButton = new JButton();
			searchButton.setBounds(new Rectangle(40, 50, 107, 29));
			searchButton.setText("Search");
			searchButton.addActionListener(new java.awt.event.ActionListener() 
			{
				public void actionPerformed(java.awt.event.ActionEvent e) 
				{
					if (initialitoForm.equals("UpdateCustomer"))
						initialiToUpdateCustomerForm();	
					if (initialitoForm.equals("RequestAuthorization"))
						initialiToRequestAuthorizationForm();						
				}	
			});
		}
		return searchButton;
	}
	
	/**
	 * This method initializes to UpdateProductForm
	 */
	private void initialiToUpdateCustomerForm()
	{		
		try 
		{
			if (cID > 0)
			{
				if (customer.loadCustomer(cID))
				{
					customerForm.loadCustomer(customer);
					customerForm.addUpdateButton();
					this.setSize(300, 330);
					this.setTitle("CUSTOMER UPDATE");
					setContentPane(customerForm);
				}	
				else
					JOptionPane.showMessageDialog(null, "CUSTOMER NOT EXIST IN DATA BASE");
			}
			else
				JOptionPane.showMessageDialog(null, "PLZ select a client");
		} catch (Exception excp)
		{
			JOptionPane.showMessageDialog(null, "ILEGAL INPUT");	
		}											
	}
	
	/**
	 * This method initializes to RequestAuthorizationForm
	 */
	private void initialiToRequestAuthorizationForm()
	{		
		try 
		{
			if (cID > 0)
			{
				if (customer.loadCustomer(cID))
				{
					customerForm.loadCustomer(customer);
					customerForm.addAuthorizationButton();
					this.setTitle("Request Authorization");
					this.setSize(450, 600);
					setContentPane(customerForm);
				}	
				else
					JOptionPane.showMessageDialog(null, "CUSTOMER NOT EXIST IN DATA BASE");
			}
			else
				JOptionPane.showMessageDialog(null, "PLZ select a client");
		} catch (Exception excp)
		{
			JOptionPane.showMessageDialog(null, "ILEGAL INPUT");	
		}											
	}
	
	
}
